import http from './http'
import { baseUrl } from '../../config'

// 获取动漫新番banner
export const getBannerList = async (date = '') => {
  return await http({
    url: `${baseUrl}/banner?search=${date}`,
    method: 'GET',
  })
}

// 获取动漫番剧归类
export const getArchiveList = async () => {
  return await http({
    url: `${baseUrl}/years`,
    method: 'GET',
  })
}

// 近3个月新番种子链接
export const getRecentList = async () => {
  return await http({
    url: `${baseUrl}/recent`,
    method: 'GET',
  })
}

// 获取声优库列表
export const getCVList = async () => {
  return await http({
    url: `${baseUrl}/cvlist`,
    method: 'GET',
  })
}

// 根据编号获取声优关系图
export const getCVTree = async (code = '') => {
  return await http({
    url: `${baseUrl}/tree?orderId=${code}`,
    method: 'GET',
  })
}

// 获取本周番剧
export const getAnimeList = async (w = 1, uId, token, loginType) => {
  const response = await http({
    url: `/api/mikan/xinfan/week?w=${w}`,
    method: 'GET',
    headers: {
      uid: uId,
      token: token,
      loginType: loginType,
    },
  })
  return response
}

// 获取番剧列表
export const getXinFanList = async (id, ep, uId, token, loginType) => {
  const response = await http({
    url: `/api/mikan/xinfan/fanList?id=${id}&ep=${ep}`,
    method: 'GET',
    headers: {
      uid: uId,
      token: token,
      loginType: loginType,
    },
  })
  return response
}

// 用户登录
export const userLogin = async (data) => {
  try {
    return await http({
      url: `/api/mikan/loginC`,
      method: 'POST',
      data,
    })
  } catch (error) {}
}

// 用户注册
export const userRegister = async (data) => {
  try {
    return await http({
      url: `/api/mikan/registerC`,
      method: 'POST',
      data,
    })
  } catch (error) {}
}

// 检查本地缓存中是否存在用户对象
export const getUserInfoFromCache = () => {
  const user = localStorage.getItem('user')
  if (user) {
    const { uId, token, loginType } = JSON.parse(user)
    return { uId, token, loginType }
  } else {
    return null
  }
}
